package handler

import (
	"context"
	"fmt"
	"zk1/srv/dao"
	"zk1/srv/model"
	"zk1/srv/pkg"
	"zk1/srv/pkg/jwt"
	__ "zk1/srv/proto"
)

type Server struct {
	__.UnimplementedUserServerServer
}

func (s *Server) Login(_ context.Context, req *__.UserLoginReq) (*__.UserLoginResp, error) {
	fmt.Println(req.Name, req.Password)
	login, err := dao.Login(req.Name)
	if login.Id == 0 {
		return &__.UserLoginResp{
			Code: 500,
			Msg:  "用户不存在",
			Data: err.Error(),
		}, err
	}
	if login.Password != pkg.Md5(req.Password) {
		return &__.UserLoginResp{
			Code: 500,
			Msg:  "密码不正确",
			Data: "错误",
		}, err
	}
	claims := jwt.CustomClaims{ID: uint(login.Id)}

	token, err := jwt.NewJWT("2210A").CreateToken(claims)
	if err != nil {
		return &__.UserLoginResp{
			Code: 500,
			Msg:  "token错误",
			Data: err.Error(),
		}, err
	}
	return &__.UserLoginResp{
		Code: 200,
		Msg:  "ok",
		Data: token,
	}, nil
}

func (s *Server) VideoList(_ context.Context, _ *__.VideoListReq) (*__.UserLoginResp, error) {
	video := []model.Video{}

	list := dao.VideoList(&video)
	if list == false {
		return &__.UserLoginResp{
			Code: 500,
			Msg:  "列表查询失败",
			Data: "错误",
		}, nil
	}
	fmt.Println(video)
	return &__.UserLoginResp{
		Code: 200,
		Msg:  "ok",
		Data: "video",
	}, nil
}
func (s *Server) VideoListOne(_ context.Context, req *__.VideoListOneReq) (*__.UserLoginResp, error) {
	video := model.Video{}
	login, err := dao.Login1(req.Id)
	if login.Id == 0 {
		return &__.UserLoginResp{
			Code: 500,
			Msg:  "用户未登录",
			Data: "错误",
		}, err
	}
	list := dao.VideoListOne(&model.Video{Id: uint(req.Id)}, &video)
	if list == false {
		return &__.UserLoginResp{
			Code: 500,
			Msg:  "视频详情查询失败",
			Data: "错误",
		}, nil
	}
	fmt.Println(video)
	return &__.UserLoginResp{
		Code: 200,
		Msg:  "ok",
		Data: video.Url,
	}, nil
}
